package com.manager.gen.module.service.enterprise;

import com.manager.commons.mybatis.dao.GenericDao;
import com.manager.commons.mybatis.service.GenericService;
import com.manager.commons.utils.StringUtil;
import com.manager.commons.utils.UUIDUtil;
import com.manager.core.web.json.AjaxJson;
import com.manager.gen.module.dao.enterprise.GenEnterpriseDao;
import com.manager.gen.module.entity.enterprise.GenEnterprise;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;


/**
 * GenEnterpriseService
 *
 * @author manager
 * @version 1.0
 */
@Service
public class GenEnterpriseService extends GenericService<GenEnterprise> {

    @Autowired
    private GenEnterpriseDao jdbcDao;

    @Override
    public GenericDao<GenEnterprise> getDao() {
        return jdbcDao;
    }

    /**
     * 保存
     *
     * @param params
     * @return
     */
    public AjaxJson save(Map<String, Object> params) {
        try {
            if (null != params) {
                String _id = String.valueOf(params.get("_id"));
                Integer c = jdbcDao.findUser(params);
                if (c == null || c == 0) {
                    int f = 0;
                    if (StringUtil.isEmpty(_id)) {
                        String uuid = UUIDUtil.getInstance().getUUID();
                        params.put("_id", uuid);
                        params.put("id", uuid);
                        params.put("password", "DD6E5E5918E94D997C686FCEBC56922F");
                        f = insertMap(params);
                    } else {
                        params.put("id", _id);
                        f = updateMap(params);
                    }
                    return new AjaxJson(f, "保存".concat(f > 0 ? "成功" : "失败"));
                } else {
                    return new AjaxJson("注册电话已被注册，请重新填写");
                }
            }
            return new AjaxJson("参数错误");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return new AjaxJson("异常错误，请稍后再试");
    }

    /**
     * 重置
     *
     * @param params
     * @return
     */
    public AjaxJson reset(Map<String, Object> params) {
        int f = 0;
        if (params != null) {
            params.put("password", "DD6E5E5918E94D997C686FCEBC56922F");
            f = jdbcDao.resetPassword(params);
        }
        return new AjaxJson(f, f > 0 ? "密码重置成功" : "密码重置失败");
    }
}
